package com.example.com.skills_utf8;



import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;




public class FragmentStack extends Activity
{
	
	int mStackLevel = 1;
	FragmentStack activity = this;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.fragment_stack);
		
		
		
		Button fragment1 = (Button)findViewById(R.id.fragment1);
		Button fragment2 = (Button)findViewById(R.id.fragment2);
		
		
		fragment1.setOnClickListener( new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				
				FragmentManager fm =activity.getFragmentManager();
				
				
				Fragment newFragment = CountingFragment.newInstance(1);
	            FragmentTransaction ft = getFragmentManager().beginTransaction();
	     //       ft.add(R.id.simple_fragment, newFragment).commit();
			
	            ft.replace(R.id.simple_fragment, newFragment).commit();
			}
		});
		
		
		fragment2.setOnClickListener( new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				
				FragmentManager fm =activity.getFragmentManager();
				
				
				Fragment newFragment = CountingFragment.newInstance(2);
	            FragmentTransaction ft = getFragmentManager().beginTransaction();
	  //          ft.add(R.id.simple_fragment, newFragment).commit();
				
	            ft.replace(R.id.simple_fragment, newFragment).commit();
	            
			}
		});
		
		
		
		/* OK
		if( savedInstanceState == null){
			FragmentManager fm =this.getFragmentManager();
			
			
			Fragment newFragment = CountingFragment.newInstance(mStackLevel);
            FragmentTransaction ft = getFragmentManager().beginTransaction();
            ft.add(R.id.simple_fragment, newFragment).commit();
			
            
            
		}
		else{
			mStackLevel = savedInstanceState.getInt("level");
		}
		*/
		
		
		
		
		
	}
	
	
	@Override
	protected void onSaveInstanceState(Bundle outState) {
		// TODO Auto-generated method stub
		super.onSaveInstanceState(outState);
		
		outState.putInt("level", mStackLevel);
	}
	
	
	
	
	
	
	
	 public static class CountingFragment extends Fragment {
	        int mNum;

	        /**
	         * Create a new instance of CountingFragment, providing "num"
	         * as an argument.
	         */
	        static CountingFragment newInstance(int num) {
	            CountingFragment f = new CountingFragment();
	            
	            // Supply num input as an argument.
	            Bundle args = new Bundle();
	            args.putInt("num", num);
	            f.setArguments(args);

	            return f;
	        }

	        /**
	         * When creating, retrieve this instance's number from its arguments.
	         */
	        @Override
	        public void onCreate(Bundle savedInstanceState) {
	            super.onCreate(savedInstanceState);
	            mNum = getArguments() != null ? getArguments().getInt("num") : 1;
	        }

	        /**
	         * The Fragment's UI is just a simple text view showing its
	         * instance number.
	         */
	        @Override
	        public View onCreateView(LayoutInflater inflater, ViewGroup container,
	                Bundle savedInstanceState) {

	        	///*
	        	View v = inflater.inflate(R.layout.hello_world, container, false);
	            View tv = v.findViewById(R.id.text);
	            ((TextView)tv).setText("Fragment #" + mNum);
	            tv.setBackgroundDrawable(getResources().getDrawable(android.R.drawable.gallery_thumb));
	        	
	        	
	            return v;
	         //*/
	       
	        	/* OK
	        	View v = inflater.inflate(R.layout.activity_opengl, null);
	        	
	        	return v;
	        	*/
	        	
	        }
	    }

	 
	 
	
}